package com.hz.erp.controller;

import com.hz.erp.constant.RetResponse;
import com.hz.erp.constant.RetResult;
import com.hz.erp.entity.dto.UserDto;
import com.hz.erp.entity.page.PageRequest;
import com.hz.erp.entity.page.PageResult;
import com.hz.erp.entity.vo.UserVo;
import com.hz.erp.enums.commons.RegistrationWayEnum;
import com.hz.erp.service.UserService;
import com.hz.erp.utils.SendSmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 柯鑫
 * @ClassName com.hz.erp.controller.UserController
 * @description 用户服务管理控制类
 * @create 2020年06月04日 16:00
 */
@RestController
@RequestMapping("user")
@Api(description = "用户操作接口")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据id查询user对象
     * @param id
     * @return
     */
    @GetMapping("findOne/{uId}")
    @ApiOperation(value = "根据id查询user对象", notes="根据id查询user对象")
    public RetResult<UserVo> findOneById(@PathVariable("uId") Long id){
        RetResult<UserVo> userRetResult = userService.selectUserById(id);
        return userRetResult;
    }


    /**
     * 根据条件查询所有数据
     * @param user
     * @return
     */
    @PostMapping("/findAll")
    @ApiOperation(value = "根据条件查询所有数据", notes="根据条件查询所有数据")
    public RetResult<List<UserDto>> findAllUser(@RequestBody UserVo user){
        RetResult<List<UserDto>> listRetResult = userService.selectUserAll(user);
        return listRetResult;
    }

    /**
     * 根据分页条件查询所有用户数据
     * @param pageRequest
     * @param userVo
     * @return
     */
    @GetMapping("findAllUserPage")
    @ApiOperation(value = "分页查询所有用户信息", notes="分页查询所有用户信息")
    public RetResult<PageResult> findUserPage(PageRequest pageRequest, UserVo userVo){

        return userService.findUserListPage(pageRequest, userVo);
    }

    /**
     * 注册User用户
     * @param user
     * @return
     */
    @PostMapping("registerErp")
    public RetResult<String> registerErp(UserVo user, HttpServletRequest request){
        RetResult<String> retResult = userService.registerUserOne(user, RegistrationWayEnum.COME_ERP.getCode(), request);
        return retResult;
    }

    /**
     * 注册User用户
     * @param user
     * @return
     */
    @PostMapping("registerAndroid")
    public RetResult<String> registerAndroid(UserVo user, HttpServletRequest request){
        RetResult<String> retResult = userService.registerUserOne(user, RegistrationWayEnum.COME_ANDROID.getCode(), request);
        return retResult;
    }

    /**
     * 登录
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/loginErp")
    public RetResult<String> loginErp(UserVo user, HttpServletRequest request){
        return userService.loginSelect(user, request, RegistrationWayEnum.COME_ERP.getCode());
    }

    @PostMapping("/loginAndroid")
    public RetResult<String> loginUser(UserVo user, HttpServletRequest request){
        return userService.loginSelect(user, request, RegistrationWayEnum.COME_ANDROID.getCode());
    }

    /**
     * 更新用户信息
     * @return
     */
    @PostMapping("/updateUserInfoById")
    public RetResult<String> updateUserInfo(UserVo user){
        return userService.updateUserInfo(user);
    }

    /**
     * 删除账号
     * @param id
     * @return
     */
    @GetMapping("deleteUserInfo/{childId}")
    public RetResult<String>  deleteUserInfo(@PathVariable("childId") Long id){
        return userService.deleteUserInfo(id);
    }

    /**
     * 退出登录
     * @param id
     * @param request
     * @return
     */
    @GetMapping("loginOut/{userId}")
    public RetResult<String> loginOut(@PathVariable("userId") Long id,  HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return RetResponse.makeOKRsp("退出登录成功");
    }


    /**
     * 发送短信验证码
     * @return
     */
    @GetMapping("/sendCode/{number}")
    public RetResult<String> sendCode(@PathVariable("number") String number,  HttpServletRequest httpServletRequest){

        String s = SendSmsUtil.sendMessage(number, httpServletRequest);
        if("OK".equals(s)){
          return RetResponse.makeOKRsp("发送成功");
        }
       return RetResponse.makeErrRsp("发送失败");
    }


    /**
     * 每次更新的版本信息
     * @return
     */
    @GetMapping("/showVersion")
    public RetResult<Map> showVersion(){
        Map map = new HashMap<>();
        map.put("version", "0.0.1");
        map.put("tem", "温度信息系统");
       return RetResponse.makeOKRsp(map);
    }


    @PostMapping("/updatePwdByLoginName")
    public RetResult<String> updatePwdByLoginName(UserVo userVo,  HttpServletRequest httpServletRequest){
      return   userService.updateUserByInfo(userVo, httpServletRequest);
    }

}
